# -*- encoding: utf-8 -*-
"""
File Name: launch
Description:
Author : 'lirui'
date: 2024/2/1
Contact: marjey_lee@163.com
Change Activity:
2024/2/1:
"""
from fastapi import FastAPI
import uvicorn
import ll
from fastapi.middleware.cors import CORSMiddleware
from projects.monitoring.dedicated_line_monitor.services.compare_cmdb_and_device_configuration import CompareCMDBAndDeviceConfig
from projects.monitoring.dedicated_line_monitor.services.dedicated_line_monitor_pipeline import DedicatedLineMonitorPipeline

_APP = FastAPI(
    title="Dedicated line monitor ",
    description="Dedicated line monitor ",
    version="1.0.1",
)
_MONITOR = DedicatedLineMonitorPipeline()
_MONITOR.start()
_APP.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)


@_APP.get("/dedicated_line/{name}/{ip}")
def read_item(name: str, ip: str):
    k = ip + '-' + name  # router name
    if k not in _MONITOR.results:
        return 0
    return int(_MONITOR.results[k])


@_APP.get("/dedicated_line_cmdb_info")  # http://189.12.16.41:902/dedicated_line_cmdb_info
def dedicated_line_cmdb_info():
    compare = CompareCMDBAndDeviceConfig()
    not_in_cmdb, redundant_in_cmdb = compare.compare()
    return {
        "not_in_cmdb": not_in_cmdb,
        "redundant_in_cmdb": redundant_in_cmdb
    }


@_APP.get("/health_check")
async def main():
    return _MONITOR.is_running_normal


def _main():
    uvicorn.run(_APP, host="0.0.0.0", port=8080)


if __name__ == '__main__':
    _main()
